package cn.ehanghai.htz.service;


import cn.ehanghai.htz.common.utils.DateUtil;
import cn.ehanghai.htz.domain.Position;
import cn.ehanghai.htz.mapper.PositionMapper;

import cn.ehanghai.htz.vo.ShipVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: xumf
 * @Date: 2019/7/5 13:22
 */
@Service
public class PositionService{

    @Autowired
    private PositionMapper positionMapper;

    public void insert(Position position){
        positionMapper.insert(position);
    }

    public Position selectBymmsi(Integer mmsi){
        Position position=new Position();
        position.setMmsi(mmsi);
        Position p= positionMapper.selectOne(position);
        return p;
    }

    public void updatePosition(Position p){
        positionMapper.updateValue(p);
    }

    public List<ShipVo> selectList(){
        List<Position> list= positionMapper.selectAll();
        List<ShipVo> shipVoList=new ArrayList<>();
        for (Position p:list) {
            ShipVo vo=new ShipVo();
            BeanUtils.copyProperties(p,vo);
            vo.setUpdateDate(DateUtil.getDateByString(p.getUpdateDate(),"yyyy-MM-dd HH:mm:ss").getTime());
            vo.setVirtual(true);
            vo.setIState(-888);
//            vo.setLat(p.getLat()+2);
//            vo.setLon(p.getLon()+2);
            String mmsi=p.getMmsi().toString();
            String code=mmsi.substring(mmsi.length()-4,mmsi.length());
            if(code.equals("0001")){
                vo.setColor("#00FF00");
            }
            shipVoList.add(vo);
//            p.setLat(p.getLat()+2);
//            p.setLon(p.getLon()+2);
//            p.setUpdateDate(DateUtil.dateToString(new Date()));
            positionMapper.updateValue(p);
        }
       return shipVoList;
    }


}
